package com.pond.pond.controller;

import com.pond.common.core.web.controller.BaseController;
import com.pond.common.core.web.domain.AjaxResult;
import com.pond.common.core.web.page.TableDataInfo;
import com.pond.common.log.annotation.Log;
import com.pond.common.log.enums.BusinessType;
import com.pond.common.security.annotation.RequiresPermissions;
import com.pond.pond.domain.PondOrderItem;
import com.pond.pond.domain.dto.CreateOrderRequest;
import com.pond.pond.domain.vo.OrderVO;
import com.pond.pond.mapper.PondOrderItemMapper;
import com.pond.pond.service.IPondOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 订单消费记录管理控制器
 */
@Tag(name = "订单消费记录管理", description = "订单消费记录相关接口")
@RestController
@RequestMapping("/order/consumption")
@Validated
public class PondConsumptionController extends BaseController {
    
    @Autowired
    private IPondOrderService pondOrderService;
    
    @Autowired
    private PondOrderItemMapper pondOrderItemMapper;
    
    /**
     * 查询订单消费记录列表
     */
    @Operation(summary = "查询订单消费记录列表")
    @RequiresPermissions("pond:consumption:list")
    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String itemName,
            @RequestParam(required = false) String consumptionType,
            @RequestParam(required = false) Long fishTypeId,
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime) {
        startPage();
        List<OrderVO.OrderItemVO> list = pondOrderItemMapper.selectConsumptionList(
            orderNo, itemName, consumptionType, fishTypeId, beginTime, endTime);
        
        // 填充用户信息（暂时使用默认值）
        for (OrderVO.OrderItemVO item : list) {
            // TODO: 通过远程调用获取用户信息
            item.setUserName("用户");
            item.setUserPhone("138****0000");
        }
        
        return getDataTable(list);
    }
    
    /**
     * 根据订单ID查询消费记录列表
     */
    @Operation(summary = "根据订单ID查询消费记录列表")
    @RequiresPermissions("pond:consumption:list")
    @GetMapping("/order/{orderId}")
    public AjaxResult getConsumptionsByOrderId(@Parameter(description = "订单ID") @PathVariable Long orderId) {
        List<OrderVO.OrderItemVO> list = pondOrderItemMapper.selectByOrderId(orderId);
        return AjaxResult.success(list);
    }
    
    /**
     * 查询订单消费记录详细
     */
    @Operation(summary = "查询订单消费记录详细")
    @RequiresPermissions("pond:consumption:query")
    @GetMapping("/{consumptionId}")
    public AjaxResult getInfo(@Parameter(description = "消费记录ID") @PathVariable Long consumptionId) {
        PondOrderItem item = pondOrderItemMapper.selectPondOrderItemById(consumptionId);
        return item != null ? AjaxResult.success(item) : AjaxResult.error("消费记录不存在");
    }
    
    /**
     * 新增订单消费记录
     */
    @Operation(summary = "新增订单消费记录")
    @RequiresPermissions("pond:consumption:add")
    @Log(title = "订单消费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody PondOrderItem item) {
        int result = pondOrderItemMapper.insertPondOrderItem(item);
        return result > 0 ? AjaxResult.success("添加成功") : AjaxResult.error("添加失败");
    }
    
    /**
     * 添加消费记录（通过订单项请求）
     */
    @Operation(summary = "添加消费记录")
    @RequiresPermissions("pond:consumption:add")
    @Log(title = "订单消费记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addConsumptionRecord(@Valid @RequestBody CreateOrderRequest.OrderItemRequest request) {
        return pondOrderService.addOrderItem(request.getOrderId(), request);
    }
    
    
    /**
     * 修改订单消费记录
     */
    @Operation(summary = "修改订单消费记录")
    @RequiresPermissions("pond:consumption:edit")
    @Log(title = "订单消费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody PondOrderItem item) {
        int result = pondOrderItemMapper.updatePondOrderItem(item);
        return result > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }
    
    /**
     * 删除订单消费记录
     */
    @Operation(summary = "删除订单消费记录")
    @RequiresPermissions("pond:consumption:remove")
    @Log(title = "订单消费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{consumptionId}")
    public AjaxResult remove(@Parameter(description = "消费记录ID") @PathVariable Long consumptionId) {
        int result = pondOrderItemMapper.deletePondOrderItemById(consumptionId);
        return result > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
    
    /**
     * 删除单个消费记录
     */
    @Operation(summary = "删除单个消费记录")
    @RequiresPermissions("pond:consumption:remove")
    @Log(title = "订单消费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/single/{consumptionId}")
    public AjaxResult removeSingle(@Parameter(description = "消费记录ID") @PathVariable Long consumptionId) {
        int result = pondOrderItemMapper.deletePondOrderItemById(consumptionId);
        return result > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
    
}